﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Mang24k99.Model.Dissertation;
using Mang24k99.Business.Dissertation;
using System.Collections.Generic;

namespace Mang24k99.Web.Dissertation
{
    public partial class Order : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    ViewState["WebID"] = Request["WebID"].ToString();
                    selectWeb.SelectedValue = ViewState["WebID"].ToString();
                    SpecialWeb.Text = selectWeb.SelectedValue == "1" ? "FX168" : "24K99";
                }
                catch { ViewState["WebID"] = "1"; }
                BindFirstDissertation();
            }
        }

        private IList<Dissert> mDis;  //专题

        protected void selectWeb_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["WebID"] = selectWeb.SelectedValue;
            SpecialWeb.Text = selectWeb.SelectedValue == "1" ? "FX168" : "24K99";
            BindFirstDissertation();
        }

        /// <summary>
        /// 绑定该网站下的前20个专题
        /// </summary>
        private void BindFirstDissertation()
        {
            int webID = SpecialWeb.Text == "FX168" ? 1 : 2;
            mDis = BDissertations.FindDissertations(webID);
            string[] oldDisIDs;// = new string[5]{"0","0","0","0","0"};

            DropDownList1.Items.Clear();
            DropDownList2.Items.Clear();
            DropDownList3.Items.Clear();
            DropDownList4.Items.Clear();
            DropDownList5.Items.Clear();

            if (SpecialWeb.Text == "FX168")
            {
                oldDisIDs = new string[5] { "0", "0", "0", "0", "0" };
                foreach (Dissert dissert in mDis)
                {
                    if (dissert.Number < 21)//1-25为上线专题,只有上线专题可以进行专题排序
                    {
                        DropDownList1.Items.Add(new ListItem(dissert.Name.Length > 30 ? dissert.Name.Substring(0, 30) + "..." : dissert.Name, dissert.ID));
                        DropDownList2.Items.Add(new ListItem(dissert.Name.Length > 30 ? dissert.Name.Substring(0, 30) + "..." : dissert.Name, dissert.ID));
                        DropDownList3.Items.Add(new ListItem(dissert.Name.Length > 30 ? dissert.Name.Substring(0, 30) + "..." : dissert.Name, dissert.ID));
                        DropDownList4.Items.Add(new ListItem(dissert.Name.Length > 30 ? dissert.Name.Substring(0, 30) + "..." : dissert.Name, dissert.ID));
                        DropDownList5.Items.Add(new ListItem(dissert.Name.Length > 30 ? dissert.Name.Substring(0, 30) + "..." : dissert.Name, dissert.ID));

                        switch (dissert.Number)
                        {
                            case 1:
                                DropDownList1.Items[DropDownList1.Items.Count - 1].Selected = true;
                                oldDisIDs[0] = dissert.ID;
                                break;
                            case 2:
                                DropDownList2.Items[DropDownList2.Items.Count - 1].Selected = true;
                                oldDisIDs[1] = dissert.ID;
                                break;
                            case 3:
                                DropDownList3.Items[DropDownList3.Items.Count - 1].Selected = true;
                                oldDisIDs[2] = dissert.ID;
                                break;
                            case 4:
                                DropDownList4.Items[DropDownList4.Items.Count - 1].Selected = true;
                                oldDisIDs[3] = dissert.ID;
                                break;
                            case 5:
                                DropDownList5.Items[DropDownList5.Items.Count - 1].Selected = true;
                                oldDisIDs[4] = dissert.ID;
                                break;
                            default:
                                break;
                        }
                    }
                }
                if (DropDownList1.Items.Count < 5)
                    Common.MessageBox.ShowAndRedirect(this, "专题不足五个,不可以设置顺序,请先添加专题!", "Edit.aspx");
            }
            else
            {
                oldDisIDs = new string[1] { "0" };
                foreach (Dissert dissert in mDis)
                {
                    if (dissert.Number < 21)
                    {
                        DropDownList1.Items.Add(new ListItem(dissert.Name.Length > 30 ? dissert.Name.Substring(0, 30)+"..." : dissert.Name, dissert.ID));
                        if (dissert.Number == 1)
                        {
                            DropDownList1.Items[DropDownList1.Items.Count - 1].Selected = true;
                            oldDisIDs[0] = dissert.ID;
                        }
                    }
                }
                if (DropDownList1.Items.Count < 1)
                    Common.MessageBox.ShowAndRedirect(this, "没有可以设置顺序的专题,请先添加专题!","Edit.aspx");
            }
            ViewState["oldDisID"] = oldDisIDs;
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("List.aspx?WEBID=" + ViewState["WebID"].ToString() + "&PageIndex=1");
        }

        protected void OK_Click(object sender, EventArgs e)
        {
            string[] oldDisID = (string[])(ViewState["oldDisID"]);
            if (SpecialWeb.Text == "FX168")
            {//.ToString().Substring(0, ViewState["oldDisID"].ToString().Length - 1).Split('+');
                string[] newDisID = new string[5] { DropDownList1.SelectedValue, DropDownList2.SelectedValue, DropDownList3.SelectedValue, DropDownList4.SelectedValue, DropDownList5.SelectedValue };
                if (newDisID[0] == newDisID[1] || newDisID[0] == newDisID[2] || newDisID[0] == newDisID[3] || newDisID[0] == newDisID[4] || newDisID[1] == newDisID[2] || newDisID[1] == newDisID[3] || newDisID[1] == newDisID[4] || newDisID[2] == newDisID[3] || newDisID[2] == newDisID[4] || newDisID[3] == newDisID[4])
                    Common.MessageBox.Show(this,"专题顺序设置不能重复!");
                else
                {
                    BDissertations.ConfigDis(oldDisID, newDisID);
                    Common.MessageBox.ShowAndRedirect(this, "专题顺序设置成功!", "List.aspx?WEBID=" + ViewState["WebID"].ToString() + "&PageIndex=1");
                }
            }
            else
            {
                BDissertations.ConfigDis(oldDisID, new string[] { DropDownList1.SelectedValue });//new string[] { ViewState["oldDisID"].ToString() });
                Common.MessageBox.ShowAndRedirect(this, "专题顺序设置成功!", "List.aspx?WEBID=" + ViewState["WebID"].ToString() + "&PageIndex=1");
            }
        }
    }
}
